package com.ers.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.ers.dao.HrDao;
import com.ers.model.Company;
import com.ers.model.HR;
import com.ers.model.Record;
import com.ers.model.Resume;

public class HrDaoImpl implements HrDao{
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public List<HR> searchByLoginName(String loginName, String password) {
		List<HR> hrs = new ArrayList<HR>();
		if(loginName == null || password == null || loginName.equals("") || password.equals("")){
			return hrs;
		}
		hrs = (List<HR>)sessionFactory.getCurrentSession().createCriteria(HR.class).add(Restrictions.eq("loginName", loginName))
				.add(Restrictions.eq("password", password)).list();
		return hrs;
	}
	@Override
	public void save(HR hr) {
		sessionFactory.getCurrentSession().save(hr);
	}
	@Override
	public void updateResumeRecord(int id,int companyId) {
		Resume r = new Resume();
		r.setId(id);
		Company c= new Company();
		c.setId(companyId);
		Record record = new Record();
		record.setResume(r);
		record.setCompany(c);
		sessionFactory.getCurrentSession().saveOrUpdate(record);
	}
	@Override
	public void deleteRecord(int id) {
		Record record = new Record();
		record.setId(id);
		sessionFactory.getCurrentSession().delete(record);
	}
	@Override
	public void updateRecord(Record record) {
		sessionFactory.getCurrentSession().saveOrUpdate(record);
	}
	@Override
	public Record loadRecordById(int id) {
		return (Record)sessionFactory.getCurrentSession().get(Record.class, id);
	}
	@Override
	public void updatePwd(String mailbox) {
		sessionFactory.getCurrentSession().createQuery("update HR u set u.password='1' where u.mailbox=:mailbox").setString("mailbox", mailbox).executeUpdate();
	}
	

}
